﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace bind_n_find
{
    /// <summary>
    /// Navigation actions enum
    /// </summary>
    public enum nav_action_e
    {
        first_t,
        prev_t,
        next_t,
        last_t
    }



    /// <summary>
    /// Interaction logic for navigation_toolbar_ctrl.xaml
    /// </summary>
    public partial class navigation_toolbar_ctrl : UserControl
    {
        #region public delegates

        public delegate void image_changed_event_handler(object sender, image_changed_event_args e);
        
        #endregion //public delegates

        #region public events

        public event image_changed_event_handler image_changed;

        #endregion //public events

        #region private fields

        private int _image_index=0;

        /// <summary>
        /// Current image
        /// </summary>
        private BitmapImage _image;

        /// <summary>
        /// Image list
        /// </summary>
        List<image_text_item_template> _list = new List<image_text_item_template>();

        #endregion //private fields

        #region public properties

        public int image_index
        {
            get { return _image_index; }
        }

        public BitmapImage image
        {
            set
            {
                //set current image and fire 'image_changed' event
                _image = value;
                _number.Text = (_image_index+1).ToString();
                _set_enabled();
                image_changed(this, new image_changed_event_args(_image));
               
            }
        }

        public List<image_text_item_template> list
        {
            set 
            { 
                //set curent image list and current image
                _list = value;
                //_image_index = 0;
                image = _list[_image_index].image;
                _count_label.Content=Properties.Resources.Of_String+" "+_list.Count.ToString();
            }
        }
        #endregion //public properties

        #region construction and initialization
        public navigation_toolbar_ctrl()
        {
            InitializeComponent();
            _init();
            this.Loaded += new RoutedEventHandler(navigation_toolbar_ctrl_Loaded);
        }

        private void _init()
        {
            _number.IsEnabled = false;
            _number.Text = "0";
            _count_label.IsEnabled = false;
            _count_label.Content = Properties.Resources.Of_String + " 0";

        }

        #endregion //construction and initialization

        #region event handlers

        void navigation_toolbar_ctrl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// Navigation button is clicked
        /// </summary>
        /// <param name="action">
        /// navigation action (which button was clicked)
        /// </param>
        private void _on_nav_button(nav_action_e action)
        {
            try
            {
                if (_list == null)
                    return;
                if (_list.Count <= 0)
                    return;
                //set image index according to action
                switch (action)
                {
                    case nav_action_e.first_t:
                        {
                            _image_index = 0;
                            
                            break;
                        }
                    case nav_action_e.last_t:
                        {
                            _image_index = _list.Count - 1;
                            break;
                        }
                    case nav_action_e.next_t:
                        {
                            if (_image_index < (_list.Count - 1))
                            {
                                _image_index++;
                            }
                            break;
                        }
                    case nav_action_e.prev_t:
                        {
                            if (_image_index > 0)
                            {
                                _image_index--;
                            }
                            break;
                        }
                }
                //set image
                if (_image_index >= 0 && _image_index < _list.Count)
                {
                    image = _list[_image_index].image;
                    
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void first_button_Click(object sender, RoutedEventArgs e)
        {
            _on_nav_button(nav_action_e.first_t);

        }

        private void prev_button_Click(object sender, RoutedEventArgs e)
        {
            _on_nav_button(nav_action_e.prev_t);

        }

        private void next_button_Click(object sender, RoutedEventArgs e)
        {
            _on_nav_button(nav_action_e.next_t);

        }

        private void last_button_Click(object sender, RoutedEventArgs e)
        {
            _on_nav_button(nav_action_e.last_t);

        }

        /// <summary>
        /// Is fired when current image index is changed in textbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _number_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    string str = _number.Text;
                    int number = 1;
                    bool res = int.TryParse(str, out number);
                    if (res)
                    {
                        if (number >= 1 && number <= _list.Count)
                        {
                            _image_index = number - 1;
                            image = _list[_image_index].image;
                        }
                        else
                        {
                            _number.Text = (_image_index + 1).ToString();
                        }
                    }
                    else
                    {
                        _number.Text = (_image_index + 1).ToString();
                    }
                }
                catch (Exception ex) { }
            }

        }

        #endregion //event handlers

        #region methods

        private void _set_enabled()
        {
            first_button.IsEnabled = true;
            prev_button.IsEnabled = true;
            last_button.IsEnabled = true;
            next_button.IsEnabled = true;
            _number.IsEnabled = false;
            _count_label.IsEnabled = false;

            if (_image_index == 0)
            {
                first_button.IsEnabled = false;
                prev_button.IsEnabled = false;
            }
            if (_image_index == _list.Count - 1)
            {
                last_button.IsEnabled = false;
                next_button.IsEnabled = false;
            }
        }
        #endregion //methods




    }

    public class image_changed_event_args : EventArgs
    {


        public image_changed_event_args(BitmapImage image)
        {
            _image = image;
        }
        private BitmapImage _image;
        public BitmapImage image
        {
            get { return _image; }
        }
    }
}
